package com.groot.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.groot.common.util.MedicalDBUtil;
import com.groot.medical.entity.AdministrativeArea;
import com.groot.medical.entity.ChronicDisease;

public class ChronicDaoImpl implements ChronicDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	@Override
	public int getAllIllTypeCount() {
		int i =0;
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="SELECT count(1) AS id FROM t_chronictype";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		if(rs.next()){
			i=rs.getInt("id");
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return i;
	}
	@Override
	public List<ChronicDisease> findIllTypeByPage(int startIndex, int pageSize) {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql = "select * from t_chronictype limit ?,?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, startIndex);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			List<ChronicDisease> ills = new ArrayList<>();
			
			while(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				ChronicDisease ill = new ChronicDisease();
				ill.setId(rs.getInt(1));
				ill.setIllCode(rs.getString(2));
				ill.setIllName(rs.getString(3));
				ill.setPyCode(rs.getString(4));
				ill.setWbCode(rs.getString(5));
				
				
				
				
				ills.add(ill);
				
			}
			
			return ills;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}
	@Override
	public void AddIllInfo(ChronicDisease ill) {
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="insert into t_chronictype(ill_code,ill_name,py_code,wb_code)values(?,?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1,ill.getIllCode());
			ps.setString(2, ill.getIllName());
			ps.setString(3, ill.getPyCode());
			ps.setString(4, ill.getWbCode());
			ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
	}
	@Override
	public ChronicDisease findIllTypeById(String id) {
	
		
		
		
		try {
			conn = MedicalDBUtil.getConnection();
			String sql = "select * from t_chronictype where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			ChronicDisease ill = new ChronicDisease();
			if(rs.next()){
				ill.setId(rs.getInt(1));
				ill.setIllCode(rs.getString(2));
				ill.setIllName(rs.getString(3));
				ill.setPyCode(rs.getString(4));
				ill.setWbCode(rs.getString(5));
			}
			
			return ill;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}
	@Override
	public void editIllInfo(ChronicDisease ill) {
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			//UPDATE student SET name='"+name+"', age=" + age+" where name='大红'";
			String sql="update t_chronictype set ill_name=?,py_code=?,wb_code=? where id=?";
			ps=conn.prepareStatement(sql);
			
			ps.setString(1, ill.getIllName());
			ps.setString(2, ill.getPyCode());
			ps.setString(3, ill.getWbCode());
			ps.setInt(4, ill.getId());
			ps.executeUpdate();
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		
	}
	@Override
	public int deleteIllTypePage(String id) {
		int i=0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="delete from t_chronictype where id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, id);
			i=ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		return i;
	}
	@Override
	public List<ChronicDisease> findAllChronicInfo() {
	
		String sql = "select * from t_chronictype";
		
		try {
			conn = MedicalDBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			List<ChronicDisease> ills = new ArrayList<>();
			while(rs.next()){
				ChronicDisease ill = new ChronicDisease();
				ill.setId(rs.getInt(1));
				ill.setIllCode(rs.getString(2));
				ill.setIllName(rs.getString(3));
				ill.setPyCode(rs.getString(4));
				ill.setWbCode(rs.getString(5));
				ills.add(ill);
			}
			return ills;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}

}
